package com.love.cloud.log.service.impl;

import com.alibaba.fastjson2.JSON;
import com.love.cloud.common.redis.queue.annotation.QueueConsumer;
import com.love.cloud.common.redis.queue.listener.ConsumerListener;
import com.love.cloud.log.api.constant.LogConstant;
import com.love.cloud.log.api.entity.SysLog;
import com.love.cloud.log.api.entity.SysSendMsgLog;
import com.love.cloud.log.mapper.SysSendMsgLogMapper;
import com.love.cloud.log.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author hongdongqu
 */
@Component
@QueueConsumer(topic = LogConstant.MQ_TOPIC_SYSTEM_LOG_RECORD_SAVE,name = "item1",groupName = "group1")
@Slf4j
class SysLogConsumerServiceImpl implements ConsumerListener {
    @Resource
    private SysLogService sysLogService;
    @Resource
    private SysSendMsgLogMapper sysSendMsgLogMapper;

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        Map<String, String> value = message.getValue();
        String data = value.get("data");
        if (data != null) {
            SysLog sysLog = JSON.parseObject(data, SysLog.class);
            sysLogService.save(sysLog);
        }
    }

}

@Component
@QueueConsumer(topic = LogConstant.MQ_TOPIC_MESSAGE_LOG_RECORD_SAVE,name = "item1",groupName = "group1")
@Slf4j
class MsgLogConsumerServiceImpl implements ConsumerListener {
    @Resource
    private SysSendMsgLogMapper sysSendMsgLogMapper;

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        Map<String, String> value = message.getValue();
        String data = value.get("data");
        if (data != null) {
            SysSendMsgLog sysSendMsgLog = JSON.parseObject(data, SysSendMsgLog.class);
            sysSendMsgLogMapper.insert(sysSendMsgLog);
        }
    }
}
